#include "pageversion.h"
#include "ui_pageversion.h"
#include <QDebug>

PageVersion::PageVersion(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PageVersion)
{
    ui->setupUi(this);
    setAutoFillBackground(true);  //设置自动填充
    QPalette palette = BKGCLR;
    setPalette(palette);
    m_lVersionNameList << "VCU"
                       << "VSCU"
                       << "DDUL"
                       << "DDUR"
                       << "EVR"
                       << "RIOM10"
                       << "CS1"
                       << "CS2"
                       << "FCU"
                       << "ECR"
                       << "ACE"
                       << "TCDS"
                       << "LCUCOM"
                       << "LCUCTL"
                       << "PCE"
                       << "HVAC"
                       << "EDCU1"
                       << "EDCU2"
                       << "EDCU3"
                       << "EDCU4"
                       << "EDCU5"
                       << "EDCU6"
                       << "EDCU7"
                       << "EDCU8"
                       << "PIC"
                       << "BCU"
                       << "BMS"
                       << "ICDS"
                       << "ATPSoftware"
                       << "ATOSoftware"
                       << "AOMSoftware"
                       << "ATPData"
                       << "ATOData";

    m_lVersionHeader   << ""
                       << "Tc1"
                       << "Mp1"
                       << "M1"
                       << "M2"
                       << "Mp2"
                       << "Tc2";

    m_ntableRowCount = ROWSONEPAGE;
    m_ntableColumnCount = m_lVersionHeader.length();
    InitVersionTable();

}

PageVersion::~PageVersion()
{
    delete ui;
}

void PageVersion::InitVersionTable()
{
    QString tableStyle = "QTableWidget{ background-color:black; selection-background-color:rgb(215,215,215); selection-color:black;\
                                        color:white; font-size:15pt; gridline-color:white }";
    QString headerStyle = "QHeaderView::section{ background-color:black; color:white; height:34; font-size:18pt; border: 1px solid white }";
    int totalTableCnt = 0;
    for(int i=0;i<m_lVersionNameList.length();i++)
    {
        if(i%ROWSONEPAGE == 0)
        {
            totalTableCnt++;
            QTableWidget *table = new QTableWidget(m_ntableRowCount,m_ntableColumnCount,this);
            table->setGeometry(50, 80, 955, 740);
            table->verticalHeader()->setVisible(false);
            table->setSelectionBehavior(QTableWidget::SelectRows);
            table->setSelectionMode(QAbstractItemView::SingleSelection);
            table->setEditTriggers(QAbstractItemView::NoEditTriggers);
            table->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
            table->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
            for(int i=0; i<m_ntableRowCount; ++i)
                table->setRowHeight(i, 28);
            table->setColumnWidth(0, 170);
            for(int i=1; i<m_ntableColumnCount; ++i)
                table->setColumnWidth(i, 130);
            table->setHorizontalHeaderLabels(m_lVersionHeader);
            table->setStyleSheet(tableStyle);
            table->setFont(QFont("arial"));
            table->setFocusPolicy(Qt::NoFocus);
            table->horizontalHeader()->setStyleSheet(headerStyle);
            table->horizontalHeader()->setDisabled(true);
            table->horizontalHeader()->setFont(QFont("arial"));
            table->selectRow(0);
            for(int k=0; k<m_ntableRowCount; ++k)
            {
                for(int j=0; j<m_ntableColumnCount; ++j)
                {
                    QTableWidgetItem *item = new QTableWidgetItem(QString::fromUtf8(""));
                    item->setTextAlignment(Qt::AlignCenter);
                    table->setItem(k, j, item);
                }
            }
            table->setVisible(i==0);
            m_lVersionTable.append(table);
        }
        m_lVersionTable[totalTableCnt-1]->item(i%ROWSONEPAGE,0)->setText(m_lVersionNameList.at(i));
    }
    ui->totalPage->setText(QString().setNum(totalTableCnt));
}

void PageVersion::on_BtnPre_pressed()
{
    int curpage = ui->curPage->text().toInt();
    if(curpage > 1)
        curpage--;
    for(int i=0;i<m_lVersionTable.length();i++)
        m_lVersionTable[i]->setVisible(i==curpage-1);
    ui->curPage->setText(QString().setNum(curpage));
}

void PageVersion::on_BtnNext_pressed()
{
    int curpage = ui->curPage->text().toInt();
    int totalpage = ui->totalPage->text().toInt();
    if(curpage < totalpage)
        curpage++;
    for(int i=0;i<m_lVersionTable.length();i++)
        m_lVersionTable[i]->setVisible(i==curpage-1);
    ui->curPage->setText(QString().setNum(curpage));
}

void PageVersion::on_Btn_Return_clicked()
{
    emit returnToMaintMenu();
}

void PageVersion::setDevVersion(int dev, int index, uint ver, uint8_t bitcnts, const QString &format, bool xfilled_zero,int display_mode)
{
    if(dev >= Ver_Cnt || index >=  m_ntableColumnCount)
        return;

    QString strverison;
    if(dev != Ver_BMS)
    {
        QStringList formatstr = format.split(".");
        int bitsum=0;
        for(int i=0;i<formatstr.size();i++)
        {
            uint8_t move = formatstr.at(i).toUInt();
            uint16_t section = (ver>>(bitcnts-bitsum-move)) & (0xffff >> (16-move));
            if(display_mode == DEC_Display)
            {
                if(xfilled_zero && i>=1 && section < 10)
                    strverison.append(QString("0%1.").arg(section));
                else
                    strverison.append(QString("%1.").arg(section));
            }
            else if(display_mode == HEX_Display)
            {
                strverison.append(QString("%1.").arg(section,2,16));
            }
            bitsum+=move;
        }
        strverison = strverison.remove(strverison.size()-1,1);
    }
    else if(dev == Ver_BMS)
        strverison = QString("%1").arg(ver*0.1);

    m_lVersionTable[dev/m_ntableRowCount]->item(dev%m_ntableRowCount,index)->setText(strverison);
}
